package h0826;
/**
  * @description 接雨水
  * @author 不知名帅哥
  * @date 2024/8/26 21:01
  * @version 1.0
*/
public class ReceiveRainWater {
    public static void main(String[] args) {

    }
    public int trap(int[] height) {
        int n=height.length;
        int[] lMax=new int[n];
        int[] rMax=new int[n];
        lMax[0]=height[0];
        for (int i = 1; i < n; i++) {
            lMax[i]=Math.max(lMax[i-1],height[i]);
        }
        rMax[n-1]=height[n-1];
        for (int i = 0; i < n-1; i++) {
            rMax[i]=Math.max(rMax[i+1],height[i+1]);
        }
        int sum=0;
        for (int i = 1; i < n-1; i++) {
            int temp=Math.min(rMax[i], lMax[i])-height[i];
            if (temp>0){
                sum+=temp;
            }
        }
        return sum;
    }
}
